package ai.people.netmon.warning.service.impl;

import ai.people.netmon.framework.domain.warning.entity.WarningInfoCollect;
import ai.people.netmon.framework.domain.warning.entity.WarningInfoCollectDetail;
import ai.people.netmon.warning.mapper.WarningInfoCollectDetailMapper;
import ai.people.netmon.warning.mapper.WarningInfoCollectMapper;
import ai.people.netmon.warning.service.WarningInfoCollectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
* @author ligua
* @description 针对表【warning_info_collect(预警信息收藏表)】的数据库操作Service实现
* @createDate 2022-07-28 17:50:38
*/
@Service
@RequiredArgsConstructor
public class WarningInfoCollectServiceImpl extends ServiceImpl<WarningInfoCollectMapper, WarningInfoCollect>
    implements WarningInfoCollectService{

    private final WarningInfoCollectMapper warningInfoCollectMapper;

    private final WarningInfoCollectDetailMapper warningInfoCollectDetailMapper;

    @Resource
    private RedissonClient redissonClient;

    /**
     * 通过参数集合批量查询收藏记录
     *
     * @param checkParamList 检查参数集合
     * @return {@link List}<{@link WarningInfoCollect}>
     */
    @Override
    @Transactional(readOnly = true)
    public List<WarningInfoCollect> batchQueryByParamList(List<WarningInfoCollect> checkParamList) {
        List<WarningInfoCollect> result = Lists.newArrayList();
        if(CollectionUtils.isEmpty(checkParamList)){
           return Lists.newArrayList();
        }
        List<List<WarningInfoCollect>> partition = Lists.partition(checkParamList, 200);
        partition.forEach(list-> result.addAll(warningInfoCollectMapper.selectByParamList(list)));
        return result;
    }

    /**
     * 信息收藏
     *
     * @param infoCollect 信息收集
     * @return {@link Integer}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer infoCollect(WarningInfoCollect infoCollect, String rowDataJson) {
        RLock lock = redissonClient.getLock("warning:info:collect:" + infoCollect.getCollectUserId());
        try {
            if(lock.tryLock(6, TimeUnit.SECONDS)){
                WarningInfoCollect one = getOneByUidInfoIdAndPlatform(infoCollect);
                if(one == null){
                    int i = warningInfoCollectMapper.insert(infoCollect);
                    if(i > 0){
                        WarningInfoCollectDetail detail = new WarningInfoCollectDetail();
                        detail.setInfoCollectId(infoCollect.getId());
                        detail.setRowDataJson(rowDataJson);
                        detail.setCreateTime(new Date());
                        detail.setIsDelete(0);
                        return warningInfoCollectDetailMapper.insert(detail);
                    }
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if(lock.isHeldByCurrentThread()){
                lock.unlock();
            }
        }
        return 0;
    }



    /**
     * 查询单个 根据uid和infoId和信息平台
     *
     * @param infoCollect 信息收集
     * @return {@link WarningInfoCollect}
     */
    private WarningInfoCollect getOneByUidInfoIdAndPlatform(WarningInfoCollect infoCollect) {
        return warningInfoCollectMapper.selectOne(new LambdaQueryWrapper<WarningInfoCollect>()
                .eq(Objects.nonNull(infoCollect.getCollectUserId()), WarningInfoCollect::getCollectUserId, infoCollect.getCollectUserId())
                .eq(Objects.nonNull(infoCollect.getCollectInfoId()), WarningInfoCollect::getCollectInfoId, infoCollect.getCollectInfoId())
                .eq(Objects.nonNull(infoCollect.getCollectInfoPlatform()), WarningInfoCollect::getCollectInfoPlatform, infoCollect.getCollectInfoPlatform())
                .eq(WarningInfoCollect::getIsDelete, 0));
    }

    /**
     * 取消信息收藏
     *
     * @param infoCollect 信息收集
     * @return {@link Integer}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer infoCancelCollect(WarningInfoCollect infoCollect) {
        RLock lock = redissonClient.getLock("warning:info:collect:" + infoCollect.getCollectUserId());
        try {
            if(lock.tryLock(6, TimeUnit.SECONDS)){
                WarningInfoCollect one = getOneByUidInfoIdAndPlatform(infoCollect);
                if(one != null){
                    int i = warningInfoCollectMapper.updateById(WarningInfoCollect.builder().id(one.getId()).isDelete(1).build());
                    if(i > 0){
                        return warningInfoCollectDetailMapper.deleteByCollectId(one.getId());
                    }
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if(lock.isHeldByCurrentThread()){
                lock.unlock();
            }
        }
        return 0;
    }
}




